package cn.topcodes.tcsf.app.web.controller.api;

import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.app.domain.form.AppAboutSettings;
import cn.topcodes.tcsf.app.service.impl.AppSettingsServiceImpl;
import cn.topcodes.tcsf.app.service.impl.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author Unicorn
 * @date 2017/5/5
 */
@Api(tags = {"APP通用接口(其他)"})
@RestController
@RequestMapping(value = "/api/app/other", produces = "application/json")
public class OtherApi {

    @Autowired
    private AppSettingsServiceImpl appSettingsService;


    @Autowired
    private ConfigService configService;


    @ApiOperation("获取APP关于信息")
    @RequestMapping(value = "about",method = RequestMethod.GET)
    public ApiResponseEntity aboutApp() {
        AppAboutSettings appAboutSettings = appSettingsService.getAppAboutSettings();
        return ApiResponseEntity.buildOK().setBody(appAboutSettings);
    }

    public static final String FILE_PATH = "/api/app/other/dl-image";

    @RequestMapping(value = "dl-image", method = RequestMethod.GET)
    public void downloadImg(@RequestParam(required = true) String path, HttpServletResponse response) throws IOException {
        String filepath = configService.getFileFolderPath() + path;
        File file = new File(filepath);
        if (!file.exists()) {
            response.sendError(HttpStatus.SC_NOT_FOUND);
            return;
        }
        FileInputStream fin = new FileInputStream(file);
        IOUtils.copy(fin, response.getOutputStream(), 1024);
    }

}
